// pages/vip/vipBuy.js
const app=getApp();
let tool = require('../../utils/tool.js');
let md5 = require('../../utils/md5.js');
const httpUtils = require('../../utils/httpUtils.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: 'https://yxpmaterial.oss-cn-beijing.aliyuncs.com/',
    detail:'在家长端可以使用复习功能。今日复习、词义闯关、对对碰、听音识义和组合拼多种复习方法，让孩子牢牢记住学习的单词，快速提高英语成绩。',
    vipTypeList:[],
    userInfo:'',
    vip:0, //vip 0非会员 ， vip 1 是会员
    vip_state: 0, //0  非会员   1  正常   2  已过期
    vipEndTime:'',//会员有效期
    selectIndex: 0, //选中的类型下标
    vipType:0, //0包年  1半年  2月
    amount:'198', //金额
    studentInfo: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      userInfo:app.globalData.userInfo,
      studentInfo: app.globalData.studentInfo
    })
    that.getPayInfo();
  },

/**
 * 获取支付详情信息
 */
  getPayInfo:function(){
    let that =this;
    httpUtils.getPayinfo({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
    }).then((res) => {
      that.setData({
        vipTypeList: res.data.total_fee,
        detail: res.data.detail,
        vip:res.data.is_vip,
        vip_state:res.data.vip_status,
        vipEndTime:res.data.vip_end_time,
      })
    });
  },

  selectType(e){
    let that= this;
    let index = e.currentTarget.dataset.index;
    let type = e.currentTarget.dataset.type; 
    let amount = e.currentTarget.dataset.amount; 
    that.setData({
        selectIndex:index,
        vipType:type,
        amount:amount
    })
    console.log("index="+that.data.selectIndex+",viptype="+that.data.vipType);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
   
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getdata:function(){
    let that = this;
    let paramArr = {
     access_token: app.globalData.studentInfo.access_token,
      org_id: app.globalData.studentInfo.org_id,
      type:1,
      openid:app.globalData.studentInfo.openid
       
 }
    let rs = tool.getSign(paramArr, app.globalData.key); //返回 bodyParam heaer sign
    let sign = rs.sign;
    let apiUrl = app.globalData.apiURL + "user/tovip?sign=" + sign;
    let payPro = new Promise(
      function (resolve, reject) {
        wx.request({
          url: apiUrl,
          header: rs.header,
          data: rs.bodyParam,
          method: 'post',
          success(res) {
            resolve(res);
          }
        })
      }
    );
    return payPro;

  },
  topay:function(){
    let that =this;
    wx.redirectTo({
      url: './vipPay/vipPay?vipType='+that.data.vipType+"&amount="+that.data.amount+"&vip="+that.data.vip,
    })
  },
  
})